package com.jinzhi.jzweb.controller;


import com.baomidou.mybatisplus.plugins.Page;
import com.jinzhi.common.base.BaseController;
import com.jinzhi.common.domain.DictDO;
import com.jinzhi.common.service.DictService;
import com.jinzhi.common.utils.Result;
import com.jinzhi.jzweb.domain.OrganizationNewsDO;
import com.jinzhi.jzweb.domain.OrganizationRolesDO;
import com.jinzhi.jzweb.service.OrganizationNewsService;
import com.jinzhi.jzweb.service.OrganizationRolesService;
import com.jinzhi.sys.domain.UserDO;
import com.jinzhi.sys.service.UserService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <pre>
 * 考核站点表
 * </pre>
 * <small> 2020-02-24 21:00:11 | lance</small>
 */
@Controller
@RequestMapping("/jzweb/organizationAss")
public class OrganizationAssController extends BaseController {
    @Autowired
    private OrganizationNewsService organizationNewsService;
    @Autowired
    private OrganizationRolesService organizationRolesService;
    @Autowired
    private UserService userService;
    @Autowired
    private DictService dictService;

    @GetMapping()
    @RequiresPermissions("jzweb:organizationAss:organizationAss")
    String OrganizationAss(Model model) {

        List<DictDO> dicList = dictService.findByKv("type" , "station_batch");

        model.addAttribute("batchList" , dicList);
        return "jzweb/organizationAss/organizationAss";
    }

    @ResponseBody
    @GetMapping("/list")
    @RequiresPermissions("jzweb:organizationAss:organizationAss")
    public Result<Page<OrganizationNewsDO>> list(OrganizationNewsDO organizationNewsDTO, String batchTime , int pageSize, int pageNumber) {

        String startDate = null;
        String endDate = null;
        if (batchTime != null && !batchTime.equals("")){ //b.substring(0, b.indexOf("-"));  //b.substring(b.indexOf("-")+1);
            startDate = batchTime.substring(0 ,batchTime.indexOf("/"))+" 00:00:00";
            endDate =  batchTime.substring(batchTime.indexOf("/")+1)+" 23:59:59";
        }


        Map map = new HashMap();
        map.put("name", organizationNewsDTO.getName());
        map.put("checkerState", organizationNewsDTO.getCheckerState());
        map.put("startDate" ,startDate);
        map.put("endDate" , endDate);
        Page<OrganizationNewsDO> page = organizationNewsService.findPageList(map, pageNumber, pageSize);
        List list = new ArrayList();
        for (int i = 0; i < page.getRecords().size(); i++) {
            OrganizationNewsDO obj = (OrganizationNewsDO) page.getRecords().get(i);
            if (obj.getCreaterId() != null) {
                UserDO userDO = userService.selectById(obj.getCreaterId());
                if (userDO != null) {
                    obj.setCreaterName(userDO.getUsername());
                }
            }
            list.add(obj);
        }
        page.setRecords(list);

        return Result.ok(page);

    }

    @GetMapping("/info/{id}")
    @RequiresPermissions("jzweb:organizationAss:organizationAss")
    String info(@PathVariable("id") Long id, Model model) {
        OrganizationNewsDO organizationNews = organizationNewsService.selectById(id);
        OrganizationRolesDO organizationRolesDO = organizationRolesService.findOneByKv("org_id",id,"role_type",5);
        if(organizationRolesDO != null){
            organizationNews.setEnclosure(organizationRolesDO.getEnclosure());//附件
            organizationNews.setCheckerState(organizationRolesDO.getCheckerState());
            organizationNews.setRemark(organizationRolesDO.getRemark());
        }
        model.addAttribute("organizationNews", organizationNews);
        return "jzweb/organizationAss/info";
    }

}
